package com.example.demo.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.demo.dto.NumDTO;
import com.example.demo.entity.HistoryEntity;
import com.example.demo.entity.ReplaceDetail;
import com.example.demo.mapper.HistoryMapper;
import com.example.demo.mapper.ReplaceDetailMapper;
import com.example.demo.mapper.ReplaceMapper;
import com.example.demo.entity.BarCode;
import io.micrometer.common.util.StringUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ReplaceService {

    @Resource
    private ReplaceMapper replaceMapper;
    @Resource
    private ReplaceDetailMapper replaceDetailMapper;
    @Resource
    private HistoryMapper historyMapper;

    public BarCode queryNew(String oldCode) {
        List<BarCode> list = replaceMapper.selectList(Wrappers.<BarCode>lambdaQuery()
                        .eq(BarCode::getIsDel,0)//0未删除
                        .eq(BarCode::getCodeOld,oldCode)
                        .orderByDesc(BarCode::getScanNum)
                );
        if(!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    public int updateById(BarCode barCode) {
        return replaceMapper.updateById(barCode);
    }

    public int insertDetail(String oldCode,String newCode) {
        ReplaceDetail replaceDetail = new ReplaceDetail();
        replaceDetail.setCodeOld(oldCode);
        replaceDetail.setCodeNew(newCode);
        replaceDetail.setReplaceDatetime(LocalDateTime.now());
        replaceDetail.setReplaceDate(LocalDate.now());
        return replaceDetailMapper.insert(replaceDetail);
    }

    public NumDTO queryNum(String oldCode) {

        NumDTO rtnDTO = new NumDTO();

        BarCode allNum = replaceMapper.queryNum(null);
        rtnDTO.setTotalNum(allNum.getHopeNum());
        rtnDTO.setPrintNum(allNum.getScanNum());

        if(!StringUtils.isEmpty(oldCode)) {
            BarCode barCode = replaceMapper.queryNum(oldCode);
            rtnDTO.setBatchNum(barCode.getHopeNum());
            rtnDTO.setBatchPrintNum(barCode.getScanNum());
        }

        rtnDTO.setTodayNum(replaceMapper.queryTodayNum());

        return rtnDTO;
    }

    public List<BarCode> randomPrint() {
        return replaceMapper.randomPrint();
    }

    public void importData(InputStream inputStream,String fileName) throws Exception {

        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(inputStream);
        XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
        //导入时间戳
        long now = System.currentTimeMillis();

        for (Row row : xssfSheet) {
            //跳过表头
            if(row.getRowNum() == 0){
                continue;
            }
//            log.info("rowNum:{}",row.getRowNum());
            String customerId = getValue(row.getCell(0));
            String oldSku = getValue(row.getCell(1));
            String newSku = getValue(row.getCell(2));
            String productName = getValue(row.getCell(3));
            String specifications = getValue(row.getCell(4));
            Integer num = Integer.parseInt(getValue(row.getCell(5)));
            String remake = getValue(row.getCell(6));
            BarCode barCode = new BarCode();
            barCode.setFlag(now);
            barCode.setCodeOld(oldSku);
            barCode.setCodeNew(newSku);
            barCode.setHopeNum(num);
            replaceMapper.insert(barCode);
            //记录历史
            HistoryEntity historyEntity = new HistoryEntity();
            historyEntity.setCustomerId(customerId);
            historyEntity.setOldSku(oldSku);
            historyEntity.setNewSku(newSku);
            historyEntity.setProductName(productName);
            historyEntity.setSpecifications(specifications);
            historyEntity.setNum(num);
            historyEntity.setRemake(remake);
            historyEntity.setFlag(now);
            historyEntity.setCreateTime(LocalDateTime.now());
            historyMapper.insert(historyEntity);
        }
    }

    public String getValue(Cell cell) {
        CellType cellType = cell.getCellType();
        if(CellType.NUMERIC.equals(cellType)) {
            return new BigDecimal(String.valueOf(cell.getNumericCellValue())).stripTrailingZeros().toPlainString();
        } else {
            return cell.getStringCellValue();
        }
    }

}
